package gui.main_menu;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import data.Cyclist;
import data_operations.QuickSort;


public class SearchRiders implements ActionListener  {
	
	private JButton[] attributes;
	private JLabel[][] cyclistStats;
	private JPanel panel;
	private String[] attributeNames = { "LP", "Surname", "Name", "Nation", "Team", "FL", "MO", "HIL", "TT", "STA", "REC", "SP", "COB", "ID" };
	private int attributesNum = 14, cyclistNum;
	private GridBagLayout layout;
	private Font font = new Font("Times New Roman", 1, 13);
	private ArrayList<Cyclist> cyclist;

	public SearchRiders(JFrame frame, ArrayList<Cyclist> cyclist) {
		this.cyclist = cyclist;
		cyclistNum = cyclist.size();
			
		frame.getContentPane().removeAll();
		frame.getContentPane().revalidate();	
		frame.getContentPane().repaint();		
		frame.setTitle("Cycling Manager - Search Riders");
		frame.setLayout(new GridLayout(0,1));	
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.NORTHWEST;
		c.insets = new Insets(0,0,2,0);
		
		layout.columnWidths = new int[] { 40, 180, 120, 40, 180, 50, 50, 50, 50, 50, 50, 50, 50, 50 };
		layout.rowHeights = new int[cyclistNum + 1];
		
		for (int i = 0; i < cyclistNum + 1; i++)
			layout.rowHeights[i] = 25;
		
		panel = new JPanel(layout);
			
		attributes = new JButton[attributesNum];
		
		for (int i = 0; i < attributesNum; i++) {
			c.gridx = i;
			attributes[i] = new JButton(attributeNames[i]);
			attributes[i].setOpaque(true);
			attributes[i].setBackground(Color.yellow);
			attributes[i].addActionListener(this);
			attributes[i].setFont(font);
			panel.add(attributes[i], c);
		}
		
		cyclistStats = new JLabel[cyclistNum][attributesNum];
		
		for (int i = 0; i < cyclistNum; i++) {
			for (int j = 0; j < attributesNum; j++) {
				c.gridx = j;
				c.gridy = i + 1;			
				
				cyclistStats[i][j] = new JLabel();
				setAtrribute(i,j,i);
				cyclistStats[i][j].setOpaque(true);
				if (j > 4)
					cyclistStats[i][j].setHorizontalAlignment(SwingConstants.CENTER);
				else
					cyclistStats[i][j].setHorizontalAlignment(SwingConstants.LEFT);
				cyclistStats[i][j].setBackground(Color.orange);
				cyclistStats[i][j].setFont(font);
				panel.add(cyclistStats[i][j], c);
			}
		}
		
		frame.add(panel);
		
	}
	
	private void setAtrribute(int x, int y, int pos) {
		
		switch (y) {
			case 1: { cyclistStats[x][y].setText(cyclist.get(pos).getSurname());  break; }
			case 2: { cyclistStats[x][y].setText(cyclist.get(pos).getFirstName()); break; }
			case 3: { cyclistStats[x][y].setText(cyclist.get(pos).getNation()); break; }
			case 4: { cyclistStats[x][y].setText(cyclist.get(pos).getTeamName()); break; }
			case 5: { cyclistStats[x][y].setText(cyclist.get(pos).getFlat()+""); break; }
			case 6: { cyclistStats[x][y].setText(cyclist.get(pos).getMountain()+""); break; }
			case 7: { cyclistStats[x][y].setText(cyclist.get(pos).getHill()+""); break; }
			case 8: { cyclistStats[x][y].setText(cyclist.get(pos).getTimeTrial()+""); break; }
			case 9: { cyclistStats[x][y].setText(cyclist.get(pos).getStamina()+""); break; }
			case 10: { cyclistStats[x][y].setText(cyclist.get(pos).getSprint()+""); break; }
			case 11: { cyclistStats[x][y].setText(cyclist.get(pos).getRecovery()+""); break; }
			case 12: { cyclistStats[x][y].setText(cyclist.get(pos).getCobble()+""); break; }
			case 13: { cyclistStats[x][y].setText(cyclist.get(pos).getId()+""); break; }
			default: { cyclistStats[x][y].setText(" "+(x + 1)+"."); }
		}
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		
		Object src = e.getSource();
		QuickSort sort = new QuickSort();
		
		if (src == attributes[5]) {	
			setNames(sort.Sort(makeTableToSort(5) , 0, cyclistNum - 1));
		}
		else if (src == attributes[6]) {	
			setNames(sort.Sort(makeTableToSort(6) , 0, cyclistNum - 1));
		}
		else if (src == attributes[7]) {	
			setNames(sort.Sort(makeTableToSort(7) , 0, cyclistNum - 1));
		}
		else if (src == attributes[8]) {	
			setNames(sort.Sort(makeTableToSort(8) , 0, cyclistNum - 1));
		}
		else if (src == attributes[9]) {	
			setNames(sort.Sort(makeTableToSort(9) , 0, cyclistNum - 1));
		}
		else if (src == attributes[10]) {	
			setNames(sort.Sort(makeTableToSort(10) , 0, cyclistNum - 1));
		}
		else if (src == attributes[11]) {	
			setNames(sort.Sort(makeTableToSort(11) , 0, cyclistNum - 1));
		}
		else if (src == attributes[12]) {	
			setNames(sort.Sort(makeTableToSort(12) , 0, cyclistNum - 1));
		}
		else if (src == attributes[13]) {	
			setNames(sort.Sort(makeTableToSort(13) , 0, cyclistNum - 1));
		}		
	}
	
	int[][] makeTableToSort(int index) {
		int[][] tab = new int[cyclistNum][2];
		
		for (int i = 0; i < cyclistNum; i++) {
			tab[i][0] = Integer.parseInt(cyclistStats[i][index].getText());
			tab[i][1] = Integer.parseInt(cyclistStats[i][13].getText());
		}
		
		return tab;
	}
	
	void setNames(int[][] tab) {
		for (int i = 0; i < cyclistNum; i++) {
			for (int j = 0; j < attributesNum; j++) {
				setAtrribute(i,j,tab[i][1]);
			}
		}
	}
	
}
